# -*- coding: utf-8 -*-
import tkinter as tk # 调用窗口tk
from PIL import Image, ImageTk
from threading import Thread
from time import sleep # 调用系统时间戳库
import pygame.mixer as mixer
import sys
import os

audio_path = "root/Audio/"
audio_format = ".mp3"
gif_path = "root/Gif/"
gif_format = ".png"
pic_path = "root/Pic/"
pic_format = ".jpg"
cxk_list = ["鸡","你","太","美","C","t","r","l","制作人","大家好","我是","坤坤","你干嘛","哈哈","哎哟","鸡你太美"]
normal_pic = "normal"

mixer.init()
mixer.music.set_volume(1.0)

def get_resource_path(relative_path):
    if hasattr(sys, '_MEIPASS'):
        return os.path.join(sys._MEIPASS, relative_path)
    return os.path.join(os.path.abspath("."), relative_path)

def get_gif(img_path):
    img = Image.open(img_path)
    gif_num=0
    try:
        while True:
            curr = img.tell()
            name = gif_path+str(curr)+gif_format
            img.save(name)
            img.seek(curr+1)
            gif_num=curr+1
    except:
        gif_num=curr+1
    return gif_num
        
def central_win(win):
    win.resizable(0,0)                      # 不可缩放
    screenwidth = win.winfo_screenwidth()	# 获取屏幕分辨率宽
    screenheight = win.winfo_screenheight()	# 获取屏幕分辨率高
    win.update()	# 更新窗口
    width = win.winfo_width()	# 重新赋值
    height = win.winfo_height()
    size = '+%d+%d' % ((screenwidth - width)/2, (screenheight - height)/2)
    # 重新赋值大小 大小为屏幕大小/2
    win.geometry(size) 	# 以新大小定义窗口

def cxk_gui():
    root=tk.Tk()
    root.title("ikun Player By 网易独家音乐人Mike Zhou")
    root.iconphoto(False, tk.PhotoImage(file=get_resource_path('root/cxk.png')))
    mainfram=tk.Frame(root,width=500, height=550)
    mainfram.grid_propagate(0)
    mainfram.grid()
    central_win(root)    
    img=Image.open(get_resource_path("root/star.png"))
    photo = img.resize((200,200)) # 限制最大高度为580 等比缩放显示
    tkImage_cxk = ImageTk.PhotoImage(image=photo)
    tk.Label(mainfram, image=tkImage_cxk).place(x=150, y=50, width=200, height=200)
    
    global gif_stop_flag
    gif_stop_flag = 0 
    
    def play_gif(img_path,delay,w,h):
        global gif_stop_flag
        global gif_list
        gif_list = []
        gif_num = get_gif(img_path)
        j=0
        for i in range(0,gif_num):
            img=Image.open(gif_path+str(i)+gif_format)
            photo = img.resize((w,h)) # 限制最大高度为580 等比缩放显示
            tkImage_cxk = ImageTk.PhotoImage(image=photo)
            gif_list.append(tkImage_cxk)
        while True:
            tk.Label(mainfram, image=gif_list[j]).place(x=(500-w)/2, y=50, width=w, height=h)
            sleep(delay)
            j=j+1
            if j==gif_num:
                j=0
            if gif_stop_flag==1:
                break
    
    def play_gif_pics(img_path,delay,w,h):
        global gif_stop_flag
        global gif_list
        gif_list = []
        gif_num = 20
        j=0
        for i in range(0,gif_num):
            img=Image.open(img_path+str(i)+gif_format)
            photo = img.resize((w,h)) # 限制最大高度为580 等比缩放显示
            tkImage_cxk = ImageTk.PhotoImage(image=photo)
            gif_list.append(tkImage_cxk)
        while True:
            tk.Label(mainfram, image=gif_list[j]).place(x=(500-w)/2, y=50, width=w, height=h)
            sleep(delay)
            j=j+1
            if j==gif_num:
                j=0
            if gif_stop_flag==1:
                break

    def gif_start():
        # play_gif(get_resource_path("root/0.gif"),0.1,200,200)    
        play_gif_pics(get_resource_path(gif_path),0.1,200,200)   
        
    Thread(target=gif_start).setDaemon(True)
    Thread(target=gif_start).start()
        
    def play_audio(num):
        name=get_resource_path(audio_path+str(cxk_list[num])+audio_format)
        mixer.music.load(name)
        mixer.music.play()
        
    def show_pic(num):
        global tkImage_cxk
        img=Image.open(get_resource_path(pic_path+str(cxk_list[num])+pic_format))      
        photo = img.resize((200,200)) # 限制最大高度为580 等比缩放显示
        tkImage_cxk = ImageTk.PhotoImage(image=photo)
        tk.Label(mainfram, image=tkImage_cxk).place(x=150, y=50, width=200, height=200)
        
    def play_cxk(num):
        global gif_stop_flag
        gif_stop_flag=1
        play_audio(num)
        show_pic(num)
    
    tk.Button(mainfram,width=30,text=cxk_list[0],command=lambda:play_cxk(0)).place(x=50+0*100, y=300+50*0, width=100, height=50)
    tk.Button(mainfram,width=30,text=cxk_list[1],command=lambda:play_cxk(1)).place(x=50+1*100, y=300+50*0, width=100, height=50)
    tk.Button(mainfram,width=30,text=cxk_list[2],command=lambda:play_cxk(2)).place(x=50+2*100, y=300+50*0, width=100, height=50)
    tk.Button(mainfram,width=30,text=cxk_list[3],command=lambda:play_cxk(3)).place(x=50+3*100, y=300+50*0, width=100, height=50)
    tk.Button(mainfram,width=30,text=cxk_list[4],command=lambda:play_cxk(4)).place(x=50+0*100, y=300+50*1, width=100, height=50)
    tk.Button(mainfram,width=30,text=cxk_list[5],command=lambda:play_cxk(5)).place(x=50+1*100, y=300+50*1, width=100, height=50)
    tk.Button(mainfram,width=30,text=cxk_list[6],command=lambda:play_cxk(6)).place(x=50+2*100, y=300+50*1, width=100, height=50)
    tk.Button(mainfram,width=30,text=cxk_list[7],command=lambda:play_cxk(7)).place(x=50+3*100, y=300+50*1, width=100, height=50)
    tk.Button(mainfram,width=30,text=cxk_list[8],command=lambda:play_cxk(8)).place(x=50+0*100, y=300+50*2, width=100, height=50)
    tk.Button(mainfram,width=30,text=cxk_list[9],command=lambda:play_cxk(9)).place(x=50+1*100, y=300+50*2, width=100, height=50)
    tk.Button(mainfram,width=30,text=cxk_list[10],command=lambda:play_cxk(10)).place(x=50+2*100, y=300+50*2, width=100, height=50)
    tk.Button(mainfram,width=30,text=cxk_list[11],command=lambda:play_cxk(11)).place(x=50+3*100, y=300+50*2, width=100, height=50)
    tk.Button(mainfram,width=30,text=cxk_list[12],command=lambda:play_cxk(12)).place(x=50+0*100, y=300+50*3, width=100, height=50)
    tk.Button(mainfram,width=30,text=cxk_list[13],command=lambda:play_cxk(13)).place(x=50+1*100, y=300+50*3, width=100, height=50)
    tk.Button(mainfram,width=30,text=cxk_list[14],command=lambda:play_cxk(14)).place(x=50+2*100, y=300+50*3, width=100, height=50)
    tk.Button(mainfram,width=30,text=cxk_list[15],command=lambda:play_cxk(15)).place(x=50+3*100, y=300+50*3, width=100, height=50)
    
    play_audio(0)
    
    root.mainloop()

if __name__ == "__main__":
    cxk_gui()
    mixer.music.stop()
    try:
        sys.exit()
    except:
        pass
    